package com.slwish.xty.mvp.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.CreditTourismInfoModel;
import com.slwish.xty.mvp.contract.CreditTourismContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;


/**
 * 首页
 *
 * @author 陈浩
 * @version 1.0.0
 */
public class CreditTourismPresenter extends BasePresenter<CreditTourismContract.View>
        implements CreditTourismContract.Presenter {

    public CreditTourismPresenter(CreditTourismContract.View view) {
        // 初始化数据仓库
        super(view);
    }

    /**
     * 根据坐标查询商户列表
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @param page      分页
     */
    @Override
    public void searchMerchant(String longitude, String latitude, int page) {
        EasyHttp.post(UrlConfig.SEARCH_MERCHANT_BY_SITE_URL)
                .params("longitude", longitude)
                .params("latitude", latitude)
                .params("page", page + "")
                .execute(new SimpleCallBack<List<CreditTourismInfoModel>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (getView() != null)
                            getView().searchMerchantFailed();
                    }

                    @Override
                    public void onSuccess(List<CreditTourismInfoModel> list) {
                        LogUtils.i("xx", "json:" + list);
                        try {
//                            JSONObject jsonObject = JSON.parseObject(json);
//                            String resultCode = json.getCode();
//                            String resultMessage = json.getMsg();
//                            //return
//                            if (!TextUtils.equals(resultCode, "0")) {
//                                setError(resultMessage);
//                                return;
//                            }
//                            List<CreditTourismInfoModel> model = JSON.parseArray(data, CreditTourismInfoModel.class);
                            if (getView() != null) {
                                getView().searchMerchantSuccess(list);
//                                setError(R.string.login_success_text);
                            }
                        } catch (Exception e) {
                            if (getView() != null)
                                getView().searchMerchantFailed();
                        }
                    }
                });
    }

    @Override
    public void searchMerchant(String scenicName, int page) {
        EasyHttp.post(UrlConfig.SEARCH_MERCHANT_URL)
                .params("scenicName", scenicName)
                .params("page", page + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (getView() != null)
                            getView().searchMerchantFailed();
                    }
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json:" + json);

                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
//                                return;
                            }
                            List<CreditTourismInfoModel> model = JSON.parseArray(data, CreditTourismInfoModel.class);
                            if (getView() != null) {
                                getView().searchMerchantSuccess(model);
//                                setError(R.string.login_success_text);
                            }
                        } catch (Exception e) {
//                            setError(R.string.loadig_failed_text);
                            if (getView() != null)
                                getView().searchMerchantFailed();
                        }

                    }
                });
    }
}
